<template>
    <div>
        <div v-show="isShow">
            <rui-page :defines="defines">
                <rui-query :label-width="200"
                           ref="searchForm"
                           :query="queryData"
                           :query-rows="searchFormOptions"
                />
                <rui-table
                    :defineId="'voucherTemplateDefine'"
                    :fixedLeft="['action']"
                    :showIndex="false"
                    :select="false"
                    :columns="templateColumns"
                    :slots="[{key:'action',slot:'action'}]"
                    @loadDatas="queryData"
                    ref-query="searchForm"
                    ref="templateTable"
                >
                    <template slot="toolBar">
                        <Button @click="add" icon="md-add" type="primary">新增</Button>
                    </template>
                    <template slot="action" slot-scope="{ row }">
                        <div>
                            <Button @click="edit(row)" size="small" type="primary" v-if="row.status!='active'">编辑</Button>
                            <Button @click="active(row)" size="small" type="success" v-if="row.status!='active'">生效</Button>
                            <Button @click="invalidity(row)" size="small" type="error" v-if="row.status=='active'">无效</Button>
                            <Button @click="view(row)" size="small" type="primary">查看</Button>
                            <Button type="error" size="small" @click="remove(row)" v-if="row.status!='active'">删除</Button>
                            <circleLoading v-if="operationLoading"/>
                        </div>
                    </template>
                </rui-table>
            </rui-page>
        </div>
        <div v-if="!isShow">
            <voucher-basics-info ref="voucherInfoForm" :parentParams="parentParams" @backPage="backPage"></voucher-basics-info>
        </div>
    </div>
</template>
<script>
    import voucherTemplateDefine from "../defines/voucher-template";
    import circleLoading from "_c/circle-loading/circle-loading.vue";
    import voucherBasicsInfo from "./voucherBasicsInfo.vue";
    import {
        active,
        delVoucherInfo,
        getVoucherTemplateList,
        invalidity
    } from "@/projects/basic/api/contract/voucher/voucherTemplate";

    export default {
        name: "product-atom",
        components: {
            circleLoading,
            voucherBasicsInfo
        },
        data() {
            return {
                operationLoading: false,
                submitLoading: false,
                parentParams: {},
                isShow: true,
                searchFormOptions: [
                    {defineId: "voucherTemplateDefine", span: 6, fields: ["mainBusiness", "childBusiness"]},
                ],
                templateColumns: [
                    'action', 'mainBusiness', 'mainBusinessCode', 'childBusiness', 'childBusinessCode', 'description', 'status', 'createBy', 'createTime', 'updateBy', 'updateTime'
                ],
            };
        },
        computed: {
            defines() {
                return [
                    {
                        id: "voucherTemplateDefine",
                        fields: voucherTemplateDefine
                    }
                ];
            }
        },
        methods: {
            queryData(queryData) {
                console.log(queryData);
                getVoucherTemplateList(queryData).then(res => {
                    if (res.code === "0000") {
                        let {records, total} = res.data;
                        this.$refs.templateTable.updateTableData(records, total);
                    }
                });
            },
            refresh() {
                this.$refs['templateTable'].reloadData();
            },
            remove(v) {
                this.$Modal.confirm({
                    title: "确认删除",
                    content: "您确认要删除子业务事件: " + v.childBusiness + " ?",
                    onOk: () => {
                        this.operationLoading = true;
                        delVoucherInfo(v.id).then(res => {
                            this.operationLoading = false;
                            if (res.code === "0000") {
                                this.$Message.success("删除成功");
                                this.refresh();
                            }
                        }).catch(() => {
                            this.operationLoading = false;
                        });
                    }
                });
            },
            //设置无效
            invalidity(v) {
                this.$Modal.confirm({
                    title: "设置无效",
                    content: "您确认要设置成无效?",
                    onOk: () => {
                        this.operationLoading = true;
                        invalidity(v.id).then(res => {
                            this.operationLoading = false;
                            if (res.code === "0000") {
                                this.$Message.success("无效成功");
                                this.refresh();
                            }
                        }).catch(() => {
                            this.operationLoading = false;
                        });
                    }
                });
            },
            //设置生效
            active(v) {
                this.$Modal.confirm({
                    title: "设置生效",
                    content: "您确认要设置成生效?",
                    onOk: () => {
                        this.operationLoading = true;
                        active(v.id).then(res => {
                            this.operationLoading = false;
                            if (res.code === "0000") {
                                this.$Message.success("生效成功");
                                this.refresh();
                            }
                        }).catch(() => {
                            this.operationLoading = false;
                        });
                    }
                });
            },
            add() {
                this.parentParams = {status: 'new', id: null, childBusinessCode: null};
                this.isShow = false;
            },
            edit(row) {
                this.parentParams = {status: 'edit', id: row.id, childBusinessCode: row.childBusinessCode};
                this.isShow = false;
            },
            view(row){
                this.parentParams = {status: 'view', id: row.id, childBusinessCode: row.childBusinessCode};
                this.isShow = false;
            },
            backPage() {
                this.isShow = true;
                this.refresh();
            }

        },
        mounted() {
            this.refresh();
        }
    };
</script>
